import 'package:flutter/material.dart';

class BaseModel extends ChangeNotifier {
  bool _busy = false;
  String _errorMessage = '';

  bool get busy => _busy;
  String get errorMessage => _errorMessage;

  void setBusy(bool value) {
    _busy = value;
    notifyListeners();
  }

  void setError(String message) {
    _errorMessage = message;
    notifyListeners();
  }

  // 用于清除错误信息
  void clearError() {
    _errorMessage = '';
    notifyListeners();
  }

  // 在执行可能改变数据状态的操作时使用，帮助自动管理_busy状态
  Future<void> runBusyFuture(Future future, {Function? onSuccess, Function? onError}) async {
    try {
      setBusy(true);
      await future;
      onSuccess?.call();
    } catch (e) {
      setError(e.toString());
      onError?.call(e);
    } finally {
      setBusy(false);
    }
  }
}
